package com.ghsc.ghxc.jsb.ui.message.activity

import androidx.lifecycle.lifecycleScope
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.AcivityChatConfSetBinding
import com.ghsc.ghxc.jsb.view.ConfirmCancelPopViiew
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.netease.nimlib.sdk.NIMClient
import com.netease.nimlib.sdk.msg.MsgService
import com.netease.yunxin.kit.corekit.im.repo.SettingRepo
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.message.activity
 * 类名：ChatConfSetActivity
 * 创建时间：2024/5/9 15:56
 * 创建人 @author： xiaolin
 * 描述 ：Todo...  设置
 */
class ChatConfSetActivity : BaseVMActivity<ChatConfSetViewModel, AcivityChatConfSetBinding>() {
    override fun attachLayoutRes(): Int {
        return R.layout.acivity_chat_conf_set
    }

    override fun attachVMClass(): Class<ChatConfSetViewModel>? {
       return ChatConfSetViewModel::class.java
    }
    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }


    var  isImMsg  = true; //  是否接受陌生人的消息


    override fun onResume() {
        super.onResume()
        mViewModel.onUserInfoMethod()
    }


    override fun initView() {
        binding.run {
            bar.topLeftView.setOnClickListener {
                finish()
            }
            bar.topTitle.text = "设置"
        }


        //设置消息响铃
        binding.ivSetSoundIn.isSelected = SettingRepo.getRingMode()
        binding.ivSetSoundIn.setSingleClickListener {
            showLoading()
            SettingRepo.setRingMode(!binding.ivSetSoundIn.isSelected)
            lifecycleScope.launch {
                delay(1500) // 延时1秒
                // 在这里执行需要延时的操作
                binding.ivSetSoundIn.isSelected = SettingRepo.getRingMode()
                dismissLoading()
            }
        }

        //设置消息震动
        binding.ivSetZhenDong.isSelected = SettingRepo.getVibrateMode()
        binding.ivSetZhenDong.setOnClickListener {
            showLoading()
            SettingRepo.setVibrateMode(!binding.ivSetZhenDong.isSelected)
            lifecycleScope.launch {
                delay(1500) // 延时1秒
                // 在这里执行需要延时的操作
                binding.ivSetZhenDong.isSelected = SettingRepo.getVibrateMode()
                dismissLoading()
            }
        }

        binding.llCleanUp.setOnClickListener {
            //清楚融云所有未读数量
            XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter  )
                .customHostLifecycle(lifecycle)
                .navigationBarColor(getCompatColor(R.color.bg_dark_color))
                .asCustom(
                    ConfirmCancelPopViiew(
                        this, "提示", "确认要清除所有未读吗？",
                        btnConfirm = {
                            NIMClient.getService(MsgService::class.java).clearAllUnreadCount()
                            showToast("清除成功")
                        },
                        btnCancel = {
                        },
                    )
                )
                .show()



        }

        binding.llMoShengRen.setSingleClickListener {
            //是否接受陌生人消息
            val map: HashMap<String, Any> = HashMap()
            map["ifstranger"] =if(isImMsg) 0 else 1
            GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onUpdateStrangerMethod(it1) }
        }

    }
    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@ChatConfSetActivity) {
                dismissLoading()
                showToast(it)
            }
            againLoginMsg.observe(this@ChatConfSetActivity) {
                dismissLoading()
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }


            onUpdateStrangerData.observe(this@ChatConfSetActivity){
                mViewModel.onUserInfoMethod()
            }

            onUserInfoData.observe(this@ChatConfSetActivity) {
                isImMsg= it.data.data.ifstranger==1
                binding.ivSetMoShengRen.isSelected = isImMsg

            }
        }
    }

}